# -*- coding: utf-8 -*-
import json
import logging


def handler(event, context):
    logger = logging.getLogger()

    logger.info("print event payload:")
    if event:  # 检查是否为空
        try:
            json_data = json.loads(event)
            logger.info(json.dumps(json_data, indent=4))
        except json.JSONDecodeError as e:
            logger.info(
                "Event is not of JSON type, print raw event data: %s", event)
    else:
        logger.info("event is empty")

    # 测试 CloudEvents 支持（CloudEvents 相关参数编码在 headers 中）
    logger.info("print headers:")
    headers_map = context.headersMap  # 遵循PEP 8命名规范
    logger.info(json.dumps(headers_map, indent=4))
    return 'hello CTYun faas\n'
